<?php
require_once(OC::$SERVERROOT."/apps/jobposition/lib/helper.php");
$item = $_['jobdetails'];
$interviews = json_decode($item->online_interview_requirements);
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
   $("#id_state").jobLocation({
     'getcityurl': getcityurl,
     'getprovinceurl': getprovinceurl
   });  
   
   $.validator.addMethod(
      "agerange",
      function(value, element, param) {
         var startagefield = $(element).hasClass("startage") ? $(element) : $(element).prevAll('.startage'),
             endagefield = $(element).hasClass("endage") ? $(element) : $(element).nextAll('.endage'),
             startvalue = $.trim(startagefield.val()), 
             endvalue = $.trim(endagefield.val()),
             valid = true; 

         if(startvalue.length <= 0 && endvalue.length <= 0)
            valid = true;
         else {
            // always validate start age 
            if(!/^\d+$/.test(startvalue))
               valid = false; 
            if(startvalue < 18)
               valid = false; 

            // don't check endage field if checking start age field and it's valid 
            if($(element).hasClass("startage") && valid)
               return true; 

            // don't check endage field if start field is wrong 
            if($(element).hasClass("endage") && !valid)
               return true; 

            if(!/^\d+$/.test(endvalue))
               valid = false; 
            if(endvalue > 65)
               valid = false; 
            
            if(parseInt(startvalue) > parseInt(endvalue))
               valid = false; 
         }
         return this.optional(element) || valid;
      },
      function (param, element) { 
         var startagefield = $(element).hasClass("startage") ? $(element) : $(element).prevAll('.startage'),
             endagefield = $(element).hasClass("endage") ? $(element) : $(element).nextAll('.endage'),
             startvalue = $.trim(startagefield.val()), 
             endvalue = $.trim(endagefield.val());

         if(!/^\d+$/.test(startvalue) || startvalue < 18)
             return "请输入初始年龄最小为18的整数"; 

         if(!/^\d+$/.test(endvalue) || endvalue > 65)
             return "请输入年龄最大为65的整数"; 
            
         if(parseInt(startvalue) > parseInt(endvalue))
             return "请输入合法的年龄要求范围";

         // should not reach here but default message 
         return "请输入合法的数字"
      }
   );

   $.validator.addClassRules("agerange", {agerange: true}); 

   $("#jobdemandsform").validate({
      submitHandler : function(form){
         var jobdemandsform = $("#jobdemandsform");
         var options = {
            dataType : 'html',
            beforeSubmit : function(arr, jobdemandsform, options){
               var datacheck = lang_balance = city_balance = langid = 0;
               var lang = new Array();
               $.each(arr, function(i, o) { if(o.value!=''){
                  datacheck++;
                  if(o.name == 'lang_name[]' && o.value != ''){ 
                     lang_balance++; 
                     lang[langid] = o.value;
                     langid++;
                  };
                  if(o.name == 'lang_level[]' && o.value != ''){ lang_balance-- };
                  if(o.name == 'id_state' && o.value != ''){ city_balance++ };
                  if(o.name == 'id_city' && o.value != ''){ city_balance-- };
               } }); 
               if(datacheck <= 2){
                  $('.showerrormes').text('请至少添加一项要求').show();
                  return false;
               }
               if(lang_balance > 0){
                  $('.showerrormes').text('请选择外语熟练度').show();
                  return false;
               } else if(lang_balance < 0){
                  $('.showerrormes').text('请选择外语种类').show();
                  return false;
               }
               if(city_balance != 0){
                  $('.showerrormes').text('请补全工作地点').show();
                  return false;
               }
               if(langid == 2){
                  if(lang[0] == lang[1]){
                     $('.showerrormes').text('语言要求重复, 请修改').show();
                     return false;
                  }
               }
               $('.error').hide();
               showLoading($("#jobdemandsform"), '', null, {opacity:"0.5"});
            },
            success : function(data){
               try {
                  var data = $.parseJSON(data); 
                  if(data.error){
                     $('.showerrormes').text(data.error).show();
                  }
               } catch(e) {
                  // html content 
                  //update tab headers 
                  $('#jobposhead').find("a.colorgray").removeClass("colorgray");
                  var curtab = $('#jobposhead').addClass('has_interview').find('li.cur_li').removeClass('gray'); 
                  curtab.find('i.nothavedone').removeClass('nothavedone').addClass('havedone');

                  // update tab body 
                  var demand_html = $('.set_require_edit').hide().siblings('.set_quence_content');
                  demand_html.empty().html(data).show();
                  $(".set_quence_content .control-label").tooltip();
                  $("label.key", '.set_quence_content').each(function(i, el) { beautifyTableCaption(el); });
               }
               return false;
            }
         };

         $("#jobdemandsform").ajaxSubmit(options);
         return false;
      }
   });
      
   $(document).on('keydown', '#jobdemandsform', function(event) {
     if (event.which == 13) {
         return false;
      }
   });
   
   $(document).on('click','#marrycheckbox',function(){
      var val = $('#marryinput').val();
      if(val == 0){
         $('#marryinput').val(1);
      }else{
    	 $('#marryinput').val(0);
      }
   })
}); 
</script>
<div id="interviewbody" class="rulesbody">
   <?php if(empty($interviews->onlineinterview)): ?>
      <div class="set_quence_content">
         <div class="notbegin yahei large"><i></i>您尚未设置匹配条件</div>
         <div class="settext large">请先<a href="#">设置匹配条件</a>，启用全部招聘功能</div>
         <div class="settextsmall medium">匹配条件为非公开信息，仅用于简历过滤</div>
      </div>
   <?php else: ?>
      <div class="set_quence_content">
         <?=$_['demands_html']?>
      </div>
   <?endif;?>

   <div class="set_require_edit" style="display:none;">
      <form id="jobdemandsform" name="jobdemandsform" action="<?php echo OC_Helper::linkToRoute("jobpos_jobdemandssave"); ?>" method="post">
      <div class="req_box">
         <div class="req_title clearfix"><span class="yahei large pull-left">基本要求</span><span class="num_tip">（选填）</span></div>
         <div class="form-horizontal clearfix">
         <?php
               $lang = $lang_choices =$haslang_qs = $haslang_anwsr = array();
               $skill_demands = '';
               foreach($_['jobdemands'] as $k=>$v): ?>
            <?php if($v['alias'] =='MORESKILL') continue; ?>
            <?php if($v['alias'] =='LANG'):
                        if($v['expected_answer']){
                           $haslang_qs[] = $v['id'];
                           $haslang_anwsr[] = $v['expected_answer'];
                        }
                        $lang[$v['id']] = $v['text_hr'];
                        if(empty($lang_choices)){
                           $lang_choices = json_decode($v['choices']);
                        }  ?>
            <?php  elseif($v['alias'] == 'SKILL'):?>
            <?php
                  if(!empty($v['expected_answer'])){
                     $generalecheck = $skilledcheck = $proficientecheck = '';
                     if (is_numeric($v['expected_answer'])){
                        $exp = $v['expected_answer'];
                     }else{
                        $answer = json_decode($v['expected_answer']);
                        if($answer->level == PJobpositionHelper::SKILL_LEVEL_KNOW){
                           $generalecheck = "selected";
                        }elseif($answer->level == PJobpositionHelper::SKILL_LEVEL_GENERALE){
                           $generalecheck = "selected";
                        }elseif($answer->level == PJobpositionHelper::SKILL_LEVEL_SKILLED){
                           $skilledcheck = "selected";
                        }elseif($answer->level == PJobpositionHelper::SKILL_LEVEL_PROFICIENTE){
                           $proficientecheck = "selected";
                        }
                        $exp = $answer->exp;
                     }

                     $skill_demands .= '<div class="control-group">
                                           <label class="control-label longwidth" for="" title="'.(compact_strlen($v['text_hr']) >= 12 ? $v['text_hr'] : '').'"><b class="longwidth">'.$v['text_hr'].'</b><em>:</em></label>
                                           <div class="controls longmarginleft">
                                              <select class="exp_select" name="skill_level['.$v['text_hr'].']"><option value=0>--请选择--</option><option '.$generalecheck.' value='.PJobpositionHelper::SKILL_LEVEL_GENERALE.'>普通</option><option '.$skilledcheck.' value='.PJobpositionHelper::SKILL_LEVEL_SKILLED.'>熟练</option><option '.$proficientecheck.' value='.PJobpositionHelper::SKILL_LEVEL_PROFICIENTE.'>精通</option></select>
                                              <input type="text" class="required number digits" min="1" name="skill_name['.$v['text_hr'].']" value="'.$exp.'" />
                                              <i class="inputword">月</i> <span>以上</span><label for="skill_name['.$v['text_hr'].']" generated="true" class="error" style="display:none;">该项必须填写</label><a href="javascript:;" class="trash"></a>
                                           </div>
                                         </div>';
                  }
              ?>
            <?php elseif($v['alias'] != 'SCHOOL_LEVEL'): ?>
              <div class="control-group <?php echo $v['alias'] == 'MARRIAGE' ? 'control-groupmarry' : ''?>" >
                <?php if($v['alias'] != 'MARRIAGE'):?> 
                <label class="control-label key" for=""><?php echo $v['text_hr']; ?>:</label>
                <?php endif; ?>
                <div class="controls">
                  <?php
                        if($v['alias'] == 'LOCATION'){
                           if($v['expected_answer'] && strpos($v['expected_answer'], '-')){
                              $location_demand= explode('-',$v['expected_answer']);
                              $location_demand_id = ProvinceHelper::getLocationId($location_demand[0], $location_demand[1]); 
                              if(!empty($location_demand_id)){
                                 $location = JobHelper::getSelectProvince("id_state", $location_demand_id[0]['provinceid'], 0, "");
                                 $location .= '<select id="id_city" name="id_city" defvalue="'.$location_demand_id[0]['cityid'].'"></select>';
                              } else {
                                 //when country is seaside,find out country_id
                                 $location_country_id=ProvinceHelper::getCountrylistId($location_demand[1]);
                                 $countryid=!empty($location_country_id)?$location_country_id[0]['countryid']:"";
                                 $location = JobHelper::getSelectProvince("id_state", 0, 0, "");
                                 $location .= '<select id="id_city" name="id_city" defvalue="'.$countryid.'"></select>';
                              }
                              $location .= '<input type="hidden" id="state" name="interview_id['.$v['id'].'][state]" value="'.$location_demand[0].'" />';
                              $location .= '<input type="hidden" id="city" name="interview_id['.$v['id'].'][city]" value="'.$location_demand[1].'" />';
                              echo $location;                          
                           } else {
                              $location = JobHelper::getSelectProvince("id_state", $v['ci_count']<=0 ? $item->id_state : "", 0, "");
                              $location .= '<select id="id_city" name="id_city" defvalue="'.($v['ci_count']<=0 ? $item->id_city : "").'"></select>';
                              $location .= '<input type="hidden" id="state" name="interview_id['.$v['id'].'][state]" value="'.($v['ci_count']<=0 ? $item->state : "").'" />';
                              $location .= '<input type="hidden" id="city" name="interview_id['.$v['id'].'][city]" value="'.($v['ci_count']<=0 ? $item->city : "").'" />';
                              echo $location;    
                           }
                        } elseif($v['alias'] == 'INDUSTRY'){
                           echo JobHelper::getSelectIndustry('interview_id['.$v['id'].']', $v['expected_answer'], 0, "class=''");
                        } elseif($v['alias'] == 'AGE'){
                           if($v['expected_answer']){
                              $age_demand= explode('-',$v['expected_answer']);
                              echo '<input id="interviewage" class="startage agerange" name="interview_id['.$v['id'].'][age_1]" type="text" value="'.$age_demand[0].'" /><i class="inputword">岁</i> <span>-</span> <input id="interviewage" class="endage agerange" type="text" value="'.$age_demand[1].'" name="interview_id['.$v['id'].'][age_2]" /> <i class="inputword position_long">岁</i>';   
                           } else {
                              echo '<input id="interviewage" class="startage agerange" name="interview_id['.$v['id'].'][age_1]" type="text" value="'.($v['ci_count']<=0 && !empty($item->agefrom) ? $item->agefrom : '').'" /><i class="inputword">岁</i> <span>-</span> <input id="interviewage" class="endage agerange" type="text" value="'.($v['ci_count']<=0 && !empty($item->ageto) ? $item->ageto : '').'" name="interview_id['.$v['id'].'][age_2]" /> <i class="inputword position_long">岁</i>';   
                           }
                           echo '<label for="interviewage" generated="true" class="error" style="display:none;">请输入合法的数字</label>';

                        } elseif($v['alias'] == 'SCHOOL_LEVEL') {
                           $schoollevel_dft_checked = $schoollevel_985_checked = $schoollevel_211_checked = '';
                           if($v['expected_answer']){
                              if($v['expected_answer'] == 1){
                                 $schoollevel_985_checked = 'checked';
                              } elseif($v['expected_answer'] == 2) {
                                 $schoollevel_211_checked = 'checked';
                              } else{
                                 $schoollevel_dft_checked = 'checked';
                              }
                           } else {
                              $schoollevel_dft_checked = 'checked';
                           }
                     ?>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="" <?=$schoollevel_dft_checked?> class="school_level"/><span class="fontsize">不限</span>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="1" <?=$schoollevel_985_checked?> class="school_level" style="margin-left:10px;"/><span class="fontsize">985</span>
                        <span class="level_explan">
                           <div class="hr_net_tipdialog">
                              <div class="hr_net_point"></div>
                              <div class="hr_net_tipcontent medium">
                                 <span><b>985院校</b>是985工程所涉及的39所高等院校。985工程是教育部为贯彻落实科教兴国的战略，在实施"面向21世纪教育振兴行动计划"中，重点支持部分高等学校创建世界一流大学和高水平大学。</span>
                                 <div><a href="/helps/doc/product/school_985_and_211.html#step1" target="_blank">查看院校名单</a></div>
                              </div>
                           </div>
                        </span>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="2" <?=$schoollevel_211_checked?> class="school_level" style="margin-left:10px;"/><span class="fontsize">211</span>
                        <span class="level_explan">
                           <div class="hr_net_tipdialog">
                              <div class="hr_net_point"></div>
                              <div class="hr_net_tipcontent medium">
                                 <span><b>211院校</b>是指211工程所涉及的100所左右重点大学。211工程是为落实科教兴国战略而进行的重点建设100所左右的高等学校和一批重点学科的战略工程。</span>
                                 <div><a href="/helps/doc/product/school_985_and_211.html#step2" target="_blank">查看院校名单</a></div>
                              </div>
                           </div>
                        </span>
                  <?php
                        } elseif($v['alias'] == 'SEX') {
                           $sex_dft_checked = $sex_male_checked = $sex_female_checked = '';
                           if($v['expected_answer']){
                              if($v['expected_answer'] == 1){
                                 $sex_male_checked = 'checked';
                              } elseif($v['expected_answer'] == 2) {
                                 $sex_female_checked = 'checked';
                              } else{
                                 $sex_dft_checked = 'checked';
                              }
                           } else {
                              $sex_dft_checked = 'checked';
                           }
                     ?>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="" <?=$sex_dft_checked?> class="school_level"/><span class="fontsize">不限</span>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="1" <?=$sex_male_checked?> class="school_level" style="margin-left:10px;"/><span class="fontsize">男</span>
                        <input type="radio" name="interview_id[<?=$v['id']?>]" value="2" <?=$sex_female_checked?> class="school_level" style="margin-left:10px;"/><span class="fontsize">女</span>
                  
                  <?php
                        }else if($v['alias'] == 'MARRIAGE') {
                          $marry_checked = '';
                          $val = 0;
                          if(!empty($v['expected_answer'])){
                             $marry_checked = 'checked';
                             $val = 1;
                          }
                   ?>
                        <label class="checkbox medium" style="display:inline-block; color:#999999; margin-left:15px;">
                           <input type="checkbox"  <?=$marry_checked ?> id="marrycheckbox">
                           <input type="hidden" name="interview_id[<?=$v['id']?>]" value="<?=$val ?>" id="marryinput">
                              希望了解婚育状况
                           </label>
                  <?php }else if($v['alias'] == 'ENTRY_DEADLINE') {
                           if($v['choices']!=''){
                             $choices = json_decode($v['choices']);
                             echo '<select name="interview_id['.$v['id'].']" >';
                             echo '<option value="">--不限--</option>';
                             foreach($choices as $key=>$value){
                                 if($value != '待定'){
                                    $selected = $key == $v['expected_answer'] ? 'selected' : '';
                                    echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';                                 
                                 }
                             }
                             echo '</select>';
                           } else {
                             echo '<input class="number" name="interview_id['.$v['id'].']" value="'.$v['expected_answer'].'" type="text" />';
                             if($v['unit_name']){
                                echo '<i class="inputword">'.$v['unit_name'].'</i>';
                             }
                           }
                        } else {
                           if($v['choices']!=''){
                             $choices = json_decode($v['choices']);
                             echo '<select name="interview_id['.$v['id'].']" >';
                             echo '<option value="">--不限--</option>';
                             foreach($choices as $key=>$value){
                                if($v['alias']=='DEGREE'){
                                   $selected = $key == ($v['ci_count']<=0 ? $item->id_degree_level : $v['expected_answer']) ? 'selected' : '';
                                }else{
                                   $selected = $key == $v['expected_answer'] ? 'selected' : '';
                                }
                              echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
                             }
                             echo '</select>';
                           }else{
                             if($v['alias']=='WORK_EXP'){
                                echo '<input class="number" name="interview_id['.$v['id'].']" value="'.($v['ci_count']<=0 && !empty($item->min_years) ? $item->min_years : $v['expected_answer']).'" type="text" />';
                             }elseif($v['alias']=='SALARY'){
                                echo '<input class="number" name="interview_id['.$v['id'].']" value="'.($v['ci_count']<=0 && !empty($item->max_salary) && $item->max_salary<=50000 ? $item->max_salary : $v['expected_answer']).'" type="text" />';
                             }else{
                                echo '<input class="number" name="interview_id['.$v['id'].']" value="'.$v['expected_answer'].'" type="text" />';
                             }
                             
                             if($v['unit_name']){
                                echo '<i class="inputword">'.$v['unit_name'].'</i>';
                             }
                           }
                           if($v['match_rule'] == 'LESS'){
                              echo '<span>以下</span>';
                           }elseif($v['match_rule']== 'GREATER'){
                              echo '<span>以上</span>';
                           }
                           echo '<label for="interview_id['.$v['id'].']" generated="true" class="error" style="display:none;">请输入合法的数字</label>';
                        }
                     ?>
                </div>
              </div>
            <?php endif; ?>
         <?php endforeach; ?>
         </div>
      </div>
      <div class="req_box">
         <div class="req_title clearfix"><span class="yahei large pull-left">技能要求</span>
            <div class="req_ctskillbox pull-left">
               <input type="text" placeholder="如JavaScript" value=""/>
               <a class="addskill" href="#"></a>
            </div>
            <span class="num_tip medium">按回车或点击"+"按钮添加1个技能，最多添加5个（选填）</span>
            <span class="skillerrors medium error hide"></span>
         </div>
         <div class="form-horizontal clearfix needredoui">
            <?php echo $skill_demands; ?>
         </div>
      </div>
      <div class="req_box">
         <div class="req_title clearfix"><span class="yahei large pull-left">外语要求</span><span class="num_tip">（选填）</span></div>
         <div class="form-horizontal clearfix">
           <?php 
            if($v['ci_count']<=0){
               $flangeuage_1 = PJobpositionHelper::getJobMatchFl($item->id_fl1,$item->id_fl_level1);
               $flangeuage_2 = PJobpositionHelper::getJobMatchFl($item->id_fl2,$item->id_fl_level2);               
               $haslang_qs = array(0 => $flangeuage_1['fl'],1=>$flangeuage_2['fl']);
               $haslang_anwsr = array(0 => $flangeuage_1['flevel'],1=>$flangeuage_2['flevel']);
           }
           ?>
           <?php $j=0; for($i=1; $i<=2; $i++){ ?>
           <div class="control-group">
             <label class="control-label key" for="">外语<?php echo $i;?>:</label>
             <div class="controls">
               <select name="lang_name[]">
                  <option value="">--请选择--</option>
               <?php 
                     foreach($lang as $k=>$v ){ 
                        $selected = isset($haslang_qs[$j]) ? ($k==$haslang_qs[$j] ? 'selected' : '') : ''; 
                        echo '<option '.$selected.' value="'.$k.'">'.$v.'</option>';
                     } 
                  ?>
               </select> 
               <select name="lang_level[]">
                  <option value="">--请选择--</option>
               <?php 
                     foreach($lang_choices as $k=>$v){
                        $selected = isset($haslang_anwsr[$j]) ? ($k==$haslang_anwsr[$j] ? 'selected' : '') : '' ; 
                        echo '<option '.$selected.' value="'.$k.'">'.$v.'</option>'; 
                     }
                  ?>
               </select>
             </div>
           </div>
           <?php $j++; } ?>
         </div>
      </div>
      <div class="req_box nobottomborder">
         <div class="form-horizontal clearfix">
           <div class="control-group">
             <div class="controls" style="margin-left:5px;">
               <input type="hidden" name="job_id" value="<?=$item->id?>">
               <input type="submit" class="ppp_bluebtn btn-req" value="提交"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="cancel_edit">取消</a>
               <span class="showerrormes error" style="display:none;">请至少添加一项要求</span>
             </div>
           </div>
         </div>
      </div>
      <input type="hidden" name="requesttoken" value="<?php echo $_['requesttoken'] ?>" id="requesttoken">
   </form>
   </div>
</div>
